package com.magic.smartcity.view.more.bus

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation
import androidx.recyclerview.widget.LinearLayoutManager
import com.magic.smartcity.R
import com.magic.smartcity.beans.BusLineBean
import com.magic.smartcity.databinding.FragmentBusDetailBinding
import com.magic.smartcity.view.MainActivity
import com.magic.smartcity.view.more.adapters.BusLineDeatilStationListAdapter
import com.magic.smartcity.view.more.viewmodels.BusViewModel

class BusDetailFragment : Fragment() {
    private lateinit var binding: FragmentBusDetailBinding
    private lateinit var busLineDetail:BusLineBean
    private val stationListAdapter = BusLineDeatilStationListAdapter()
    private val viewModel by lazy {
        ViewModelProvider(this).get(BusViewModel::class.java)
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        busLineDetail = arguments?.getSerializable("busLineDetail") as BusLineBean
        viewModel.getLineStationListById(busLineDetail.id)
        val mainActivity = activity as MainActivity
        mainActivity.setActionBarTitle(busLineDetail.name)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentBusDetailBinding.inflate(layoutInflater)
        initView()
        return binding.root
    }

    private fun initView() {
        //设置线路的数据
        binding.busLineDetail = busLineDetail
        //设置线路所经过的站点数据
        binding.lineDetailStationList.layoutManager = LinearLayoutManager(requireContext(),LinearLayoutManager.VERTICAL,false)
        binding.lineDetailStationList.adapter = stationListAdapter
        //获得此线路的所有站点
        viewModel.busLineStationListById.observe(viewLifecycleOwner){
            stationListAdapter.setData(it)
        }
        binding.goToSelectRunDate.setOnClickListener{
            val bundle = Bundle()
            bundle.putInt("lineId",busLineDetail.id)
            Navigation.findNavController(it).navigate(R.id.action_busDetailFragment_to_busRidingTimeFragment,bundle)
        }

    }

}